home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / idparam.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  175 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* idparam.c */
  20. /* Utilities for getting parameters out of dictionaries. */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "dict.h"
  24. #include "gsmatrix.h"        /* for dict_matrix_param */
  25. #include "dparam.h"        /* interface definition */
  26. #include "errors.h"
  27. #include "iutil.h"        /* for num_params */
  28.  
  29. /* Get a Boolean parameter from a dictionary. */
  30. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  31. int
  32. dict_bool_param(const ref *pdict, const ref *pname, int defaultval, int *pvalue)
  33. {    ref *pdval;
  34.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  35.        {    *pvalue = defaultval;
  36.         return 1;
  37.        }
  38.     if ( !r_has_type(pdval, t_boolean) )
  39.         return_error(e_typecheck);
  40.     *pvalue = pdval->value.index;
  41.     return 0;
  42. }        
  43.  
  44. /* Get an integer parameter from a dictionary. */
  45. /* Return 0 if found, 1 if defaulted, <0 if invalid. */
  46. /* Note that the default value may be out of range, in which case */
  47. /* a missing value will return e_rangecheck rather than 1. */
  48. int
  49. dict_int_param(const ref *pdict, const ref *pname, int minval, int maxval,
  50.   int defaultval, int *pvalue)
  51. {    int code;
  52.     ref *pdval;
  53.     long ival;
  54.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  55.        {    ival = defaultval;
  56.         code = 1;
  57.        }
  58.     else
  59.        {    switch ( r_type(pdval) )
  60.         {
  61.         case t_integer:
  62.             ival = pdval->value.intval;
  63.             break;
  64.         case t_real:
  65.             /* Allow an integral real, because Fontographer */
  66.             /* (which violates the Adobe specs in other ways */
  67.             /* as well) sometimes generates output that */
  68.             /* needs this. */
  69.             if ( pdval->value.realval < minval || pdval->value.realval > maxval )
  70.                 return_error(e_rangecheck);
  71.             ival = (long)pdval->value.realval;
  72.             if ( ival != pdval->value.realval )
  73.                 return_error(e_rangecheck);
  74.             break;
  75.         default:
  76.             return_error(e_typecheck);
  77.         }
  78.         code = 0;
  79.        }
  80.     if ( ival < minval || ival > maxval )
  81.         return_error(e_rangecheck);
  82.     *pvalue = (int)ival;
  83.     return code;
  84. }        
  85.  
  86. /* Get a float parameter from a dictionary. */
  87. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  88. int
  89. dict_float_param(const ref *pdict, const ref *pname, floatp defaultval, float *pvalue)
  90. {    ref *pdval;
  91.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  92.        {    *pvalue = defaultval;
  93.         return 1;
  94.        }
  95.     switch ( r_type(pdval) )
  96.        {
  97.     case t_integer: *pvalue = pdval->value.intval; return 0;
  98.     case t_real: *pvalue = pdval->value.realval; return 0;
  99.        }
  100.     return_error(e_typecheck);
  101. }        
  102.  
  103. /* Get an integer array from a dictionary. */
  104. /* Return the element count if OK, 0 if missing, <0 if invalid. */
  105. int
  106. dict_int_array_param(const ref *pdict, const ref *pname, uint maxlen, int *ivec)
  107. {    ref *pdval;
  108.     const ref *pa;
  109.     int *pi = ivec;
  110.     uint size;
  111.     int i;
  112.     if ( dict_find(pdict, pname, &pdval) <= 0 ) return 0;
  113.     if ( !r_has_type(pdval, t_array) )
  114.         return_error(e_typecheck);
  115.     size = r_size(pdval);
  116.     if ( size > maxlen )
  117.         return_error(e_limitcheck);
  118.     pa = pdval->value.const_refs;
  119.     for ( i = 0; i < size; i++, pa++, pi++ )
  120.        {    /* See dict_int_param above for why we allow reals here. */
  121.         switch ( r_type(pa) )
  122.         {
  123.         case t_integer:
  124.             if ( pa->value.intval != (int)pa->value.intval )
  125.                 return_error(e_rangecheck);
  126.             *pi = (int)pa->value.intval;
  127.             break;
  128.         case t_real:
  129.             if ( pa->value.realval < min_int ||
  130.                  pa->value.realval > max_int ||
  131.                  pa->value.realval != (int)pa->value.realval
  132.                )
  133.                 return_error(e_rangecheck);
  134.             *pi = (int)pa->value.realval;
  135.             break;
  136.         default:
  137.             return_error(e_typecheck);
  138.         }
  139.        }
  140.     return size;
  141. }        
  142.  
  143. /* Get a float array from a dictionary. */
  144. /* Return the element count if OK, <0 if invalid. */
  145. /* If the parameter is missing, then if defaultvec is NULL, return 0; */
  146. /* if defaultvec is not NULL, copy it into fvec (maxlen elements) */
  147. /* and return maxlen. */
  148. int
  149. dict_float_array_param(const ref *pdict, const ref *pname, uint maxlen, float *fvec, float *defaultvec)
  150. {    ref *pdval;
  151.     uint size;
  152.     int code;
  153.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  154.     {    if ( defaultvec == NULL ) return 0;
  155.         memcpy(fvec, defaultvec, maxlen * sizeof(float));
  156.         return maxlen;
  157.     }
  158.     if ( !r_has_type(pdval, t_array) )
  159.         return_error(e_typecheck);
  160.     size = r_size(pdval);
  161.     if ( size > maxlen )
  162.         return_error(e_limitcheck);
  163.     code = num_params(pdval->value.refs + size - 1, size, fvec);
  164.     return (code >= 0 ? size : code);
  165. }        
  166.  
  167. /* Get a matrix from a dictionary. */
  168. int
  169. dict_matrix_param(const ref *pdict, const ref *pname, gs_matrix *pmat)
  170. {    ref *pdval;
  171.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  172.         return_error(e_typecheck);
  173.     return read_matrix(pdval, pmat);
  174. }
  175.